import pygame
from pygame.locals import *
from OpenGL.GL import *
from calibration_pattern import CalibrationPattern
from calibration_circle import CalibrationCircle

SCREEN_SIZE = (1680, 1050)

def init():
	glClearColor(1.0, 1.0, 1.0, 1.0)
	glEnable(GL_LINE_SMOOTH)
	#glEnable(GL_BLEND)
	
	glHint( GL_LINE_SMOOTH_HINT, GL_NICEST)
	glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST)
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
		
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	
def main():
	#variables
	max_fps = 40	
	
	pygame.init()
	screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE | OPENGL | DOUBLEBUF)
	clock = pygame.time.Clock()
	
	init() 
	cp = CalibrationPattern(4, 100, 1)
	grid = cp.make_rects()
	grid_size = cp.get_size()
	
	cc = CalibrationCircle(20, 20, 20, screen=SCREEN_SIZE)
	while True:
		for event in pygame.event.get():
			if event.type == QUIT:
				return
			if event.type == KEYUP and event.key == K_ESCAPE:
				return
			if event.type == KEYDOWN and event.key == K_SPACE:
				cc.update_rand_pos(SCREEN_SIZE)
		
		if pygame.mouse.get_pressed()[0]:
			pos = pygame.mouse.get_pos()
			cc.update(pos[0], pos[1])
			#print cc.get_pos()
			
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		cp.draw_chessboard(grid, grid_size, SCREEN_SIZE)
		
		cc.draw_circle()
	
		pygame.display.flip()
		framerate = clock.get_fps()
		clock.tick_busy_loop(max_fps)
		pygame.display.set_caption("Calibration Pattern: @ %d" %(framerate))
		
if __name__ == '__main__':
	main()

